package com.ss.android.newmedia.a;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.plugin.interfaces.pushmanager.client.SsPushManager;
import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.SpipeCore;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.newmedia.activity.browser.BrowserActivity;
import com.ss.android.newmedia.activity.BaseActivity;
import com.ss.android.newmedia.feedback.FeedbackActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;

public abstract class AdsAppBaseActivity extends BaseActivity {
    public Uri extraSchemaUri;
    public String b;
    public String c;
    public String d;
    public Spipe e;
    public boolean f;
    public boolean g;
    public String notificationSource;
    public int i;
    private BaseAppData j;
    private boolean k;
    private int l;
    private String m;
    private boolean n;

    public AdsAppBaseActivity() {

        this.k = false;
        this.f = false;
        this.g = false;
        this.notificationSource = null;
        this.i = -1;
        this.l = -1;
        this.m = "";
        this.n = false;
    }

    public static Intent a(Context arg10, Uri arg11) {
        String v3;
        boolean v4;
        int v5;
        String v6;
        Intent v1_1;
        Intent v0 = null;
        boolean v2 = true;
        if (arg11 != null) {
            try {
                v1_1 = new Intent(arg10, BrowserActivity.class);
                v6 = arg11.getQueryParameter("url");
                if (android.support.design.a.isTextEmpty(v6)) {
                    return v0;
                }

                if (arg11 == null) {
                    v5 = 0;
                } else if (h(arg11.getQueryParameter("rotate"))) {
                    v5 = 1;
                } else {
                    v5 = 0;
                }

                if (arg11 == null) {
                    v4 = false;
                } else if (h(arg11.getQueryParameter("no_hw"))) {
                    v4 = true;
                } else {
                    v4 = false;
                }
                if (arg11 == null) {
                    v2 = false;
                } else if (!h(arg11.getQueryParameter("hide_more"))) {
                    v2 = false;
                }

                v3 = arg11.getQueryParameter("ad_id");
                if (android.support.design.a.isTextEmpty(v3)) {
//                    goto label_36;
                } else {
                    v1_1.putExtra("ad_id", Long.parseLong(v3));
                }
                v1_1.setData(Uri.parse(BaseAppData.inst().f(URLDecoder.decode(v6, "UTF-8"))));
                v1_1.putExtra("swipe_mode", 2);
                v1_1.putExtra("show_toolbar", true);
                if (v5 != 0) {
                    v1_1.putExtra("orientation", 0);
                }

                if (v4) {
                    v1_1.putExtra("bundle_no_hw_acceleration", v4);
                }

                if (v2) {
                    v1_1.putExtra("hide_more", v2);
                }

                String v2_1 = arg11.getQueryParameter("title");
                if (!android.support.design.a.isTextEmpty(v2_1)) {
                    v1_1.putExtra("title", v2_1);
                }

                v2_1 = arg11.getQueryParameter("gd_label");
                if (!android.support.design.a.isTextEmpty(v2_1)) {
                    v1_1.putExtra("gd_label", v2_1);
                }

                v2_1 = arg11.getQueryParameter("gd_ext_json");
                if (!android.support.design.a.isTextEmpty(v2_1)) {
                    v1_1.putExtra("gd_ext_json", v2_1);
                }

                v2_1 = arg11.getQueryParameter("webview_track_key");
                if (!android.support.design.a.isTextEmpty(v2_1)) {
                    v1_1.putExtra("webview_track_key", v2_1);
                }

                v2_1 = arg11.getQueryParameter("wap_headers");
                if (!android.support.design.a.isTextEmpty(v2_1)) {
                    v1_1.putExtra("wap_headers", v2_1);
                }

                a(v1_1, arg11);
                v0 = v1_1;
            } catch (Exception v1) {
                v1.printStackTrace();
            }
        }

        return v0;
    }

    public static void a(Intent arg12, Uri arg13) {
        boolean v0_4;
        int v0_3;
        boolean v1 = true;
        if (arg12 != null && arg13 != null) {
            BaseAppData v0 = BaseAppData.inst();
            if (v0.aD()) {
                arg13 = arg13.buildUpon().build();
            }

            String v0_1 = arg13.getQueryParameter("back_button_color");
            String v3 = arg13.getQueryParameter("back_button_icon");
            String v4 = arg13.getQueryParameter("back_button_position");
            String v5 = arg13.getQueryParameter("disableHistory");
            String v6 = arg13.getQueryParameter("hide_bar");
            String v7 = arg13.getQueryParameter("hide_nav_bar");
            String v8 = arg13.getQueryParameter("title");
            if (!android.support.design.a.isTextEmpty(v8)) {
                arg12.putExtra("title", v8);
            }

            v8 = arg13.getQueryParameter("status_bar_color");
            String v9 = arg13.getQueryParameter("status_bar_background");
            String v10 = arg13.getQueryParameter("hide_status_bar");
            if (!android.support.design.a.isTextEmpty(v0_1)) {
                arg12.putExtra("back_button_color", v0_1);
            }

            if (!android.support.design.a.isTextEmpty(v3)) {
                arg12.putExtra("back_button_icon", v3);
            }

            if (!android.support.design.a.isTextEmpty(v4)) {
                arg12.putExtra("back_button_position", v4);
            }

            if (!android.support.design.a.isTextEmpty(v8)) {
                arg12.putExtra("status_bar_color", v8);
            }

            if (!android.support.design.a.isTextEmpty(v9)) {
                arg12.putExtra("status_bar_background", v9);
            }

            if (!android.support.design.a.isTextEmpty(v10)) {
                try {
                    v0_3 = Integer.parseInt(v10);
                } catch (NumberFormatException v0_2) {
                    v0_2.printStackTrace();
                    v0_3 = 0;
                }

                if (v0_3 <= 0) {
//                    goto label_58;
                } else {
                    arg12.putExtra("hide_status_bar", true);
                }
            }

            label_58:
            if (!android.support.design.a.isTextEmpty(v5)) {
                try {
                    v0_3 = Integer.parseInt(v5);
                } catch (NumberFormatException v0_2) {
                    v0_2.printStackTrace();
                    v0_3 = 0;
                }

                v0_4 = v0_3 > 0 ? true : false;
                arg12.putExtra("back_button_disable_history", v0_4);
            }

            if (!android.support.design.a.isTextEmpty(v6)) {
                try {
                    v0_3 = Integer.parseInt(v6);
                } catch (NumberFormatException v0_2) {
                    v0_2.printStackTrace();
                    v0_3 = 0;
                }

                v0_4 = v0_3 > 0 ? true : false;
                arg12.putExtra("key_hide_bar", v0_4);
            }

            if (!android.support.design.a.isTextEmpty(v7)) {
                try {
                    v0_3 = Integer.parseInt(v7);
                } catch (NumberFormatException v0_2) {
                    v0_2.printStackTrace();
                    v0_3 = 0;
                }

                if (v0_3 <= 0) {
                    v1 = false;
                }

                arg12.putExtra("key_hide_bar", v1);
            }

            if (!"down_arrow".equals(v3) && !"close".equals(v3)) {
                return;
            }

            if (!"top_left".equals(v4) && !"top_right".equals(v4) && !android.support.design.a.isTextEmpty(v4)) {
                return;
            }

            arg12.putExtra("activity_trans_type", 3);
        }
    }

    public static boolean openUrl(Context context, String url, String arg7) {
        String v0 = null;
        if (!android.support.design.a.isTextEmpty(url)) {
            try {
                v0 = Uri.parse(url).getQueryParameter("ad_id");
            } catch (Exception exception) {
                if (Logger.debug()) {
                    Logger.d("AdsAppBaseActivity", "openUrl is not a url " + exception);
                }
            }
        }
        return !android.support.design.a.isTextEmpty(v0)
                ? a(context, url, arg7, Long.parseLong(v0))
                : a(context, url, arg7, 0);
    }

    private static boolean a(Context arg9, String arg10, String arg11, long arg12) {
        Intent v2_1;
        Intent v4_1;
        String v3;
        Intent v0_2;
        Uri v2;
        boolean v0 = false;
        if (arg9 == null) {
            return v0;
        }

        try {
            if (!android.support.design.a.isTextEmpty(arg10)) {
                v2 = Uri.parse(arg10);
                if (TTUtils.isValidUrl(arg10)) {
                    v0_2 = new Intent(arg9, BrowserActivity.class);
                    v0_2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    v0_2.setData(v2);
                    v0_2.putExtra("swipe_mode", 2);
                    if (!android.support.design.a.isTextEmpty((String) null)) {
                        v0_2.putExtra("bundle_download_app_log_extra", (String) null);
                    }

                    if (!android.support.design.a.isTextEmpty(arg11)) {
                        v0_2.putExtra("bundle_app_package_name", arg11);
                    }

                    v0_2.putExtra("ad_id", arg12);
                    if (!(arg9 instanceof Activity)) {
                        v0_2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    }

                    a(v0_2, v2);
                    arg9.startActivity(v0_2);
                    return true;
                } else {
                    v3 = v2.getScheme();
                    boolean v4 = c(v3);
                    String v5 = v2.getHost();
                    if ((v4) && ("webview".equals(v5))) {
                        v0_2 = a(arg9, v2);
                        if (!android.support.design.a.isTextEmpty((String) null)) {
                            v0_2.putExtra("bundle_download_app_log_extra", (String) null);
                        }

                        v0_2.putExtra("swipe_mode", 2);
                        if (!(arg9 instanceof Activity)) {
                            v0_2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        }

                        arg9.startActivity(v0_2);
                        return true;
                    }

                    if (!v4) {
//                goto label_89;
                    } else {
                        Class v0_3 = BaseAppData.inst().ab();
                        if (v0_3 != null) {
                            Intent v3_1 = new Intent(arg9, v0_3);
                            v3_1.setData(v2);
                            v3_1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            v3_1.putExtra("is_from_self", true);
                            if (!android.support.design.a.isTextEmpty((String) null)) {
                                v3_1.putExtra("bundle_download_app_log_extra", (String) null);
                            }

                            v3_1.putExtra("ad_id", arg12);
                            if (!(arg9 instanceof Activity)) {
                                v3_1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            }

                            arg9.startActivity(v3_1);
                        }
                        return true;
                    }

//            label_89:
                    v4_1 = new Intent("android.intent.action.VIEW");
                    v4_1.setData(v2);
                    v4_1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    if (!ToolUtils.isInstalledApp(arg9, v4_1)) {
//                goto label_106;
                    } else {
                        v4_1.putExtra("open_url", arg10);
                        if (!(arg9 instanceof Activity)) {
                            v4_1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        }
                        arg9.startActivity(v4_1);
                        return true;
                    }
                    if (!v3.startsWith("snssdk")) {
//                goto label_127;
                    } else {
                        v2_1 = new Intent("com.ss.android.sdk." + v3);
                        if (!ToolUtils.isInstalledApp(arg9, v2_1)) {
//                    goto label_127;
                        } else {
                            v2_1.putExtra("open_url", arg10);
                            if (!(arg9 instanceof Activity)) {
                                v2_1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            }

                            arg9.startActivity(v2_1);
                            return true;
                        }
                    }
                }
            }

            if (android.support.design.a.isTextEmpty(arg11)) {
                return v0;
            }

            if (!ToolUtils.isAppInstalled(arg9, arg11)) {
                return v0;
            }
            arg9.startActivity(ToolUtils.getLaunchIntentForPackage(arg9, arg11));
            return true;
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }
        return true;
    }

    public final int a(String arg3, int arg4) {
        int v0 = this.f(arg3);
        if (v0 != -1) {
            arg4 = v0;
        }

        return arg4;
    }

    public abstract void startAppActivity();

    public boolean a(Uri arg2) {
        return false;
    }

    public static String b() {
        return "snssdk" + SpipeCore.getAppId();
    }

    public static String b(String arg3) {
        if (!android.support.design.a.isTextEmpty(arg3)) {
            try {
                String v0_1 = Uri.parse(arg3).getScheme();
                String v1 = "snssdk" + SpipeCore.getAppId();
                if (!"sslocal".equals(v0_1) && !"localsdk".equals(v0_1)) {
                    return arg3;
                }

                arg3 = arg3.replace(((CharSequence) v0_1), ((CharSequence) v1));
            } catch (Exception v0) {
            }
        }

        return arg3;
    }

    public static boolean c(String arg4) {
        boolean v0 = false;
        if (!android.support.design.a.isTextEmpty(arg4)) {
            if ("sslocal".equals(arg4)) {
                v0 = true;
            } else {
                String v2 = b();
                if (!android.support.design.a.isTextEmpty(v2) && (v2.equals(arg4))) {
                    v0 = true;
                }
            }
        }

        return v0;
    }

    public final boolean isActivityInThirdAppTask() {

        try {
            List v0_1 = ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE)).getRecentTasks(2, 2);
            if (v0_1 == null) {
                return false;
            }

            Iterator v2 = v0_1.iterator();

            Object v0_2;
            do {
                label_8:
                if (!v2.hasNext()) {
                    return false;
                }

                v0_2 = v2.next();
                if (((ActivityManager.RecentTaskInfo) v0_2).id != this.getTaskId()) {
                    continue;
                }

                if (v0_2 == null) {
                    continue;
                }

                if (((ActivityManager.RecentTaskInfo) v0_2).baseIntent == null) {
                    continue;

                }
            }
            while (((ActivityManager.RecentTaskInfo) v0_2).baseIntent.getComponent() == null);

            if (this.getPackageName().equals(((ActivityManager.RecentTaskInfo) v0_2).baseIntent.getComponent().getPackageName())) {
                return false;
            }
        } catch (Exception v0) {
            return false;
        }

        boolean v0_3 = true;
        return v0_3;
    }

    private boolean d() {
        boolean v0 = false;
        if (Logger.debug()) {
            Logger.d("AdsAppBaseActivity", "startCommonActivity start");
        }

        Intent v2 = null;
        if (android.support.design.a.isTextEmpty(this.b)) {
            v2 = ToolUtils.getLaunchIntentForPackage(((Context) this), this.getPackageName());
        } else if ("feedback".equals(this.b)) {
            v2 = new Intent(((Context) this), FeedbackActivity.class);
            v2.putExtra("key_appkey", this.j.getAppContext().getFeedbackAppKey());
        }

        if (v2 != null) {
            if (this.g) {
                v2.putExtra("from_notification", true);
                if (!android.support.design.a.isTextEmpty(this.notificationSource)) {
                    v2.putExtra("notification_source", this.notificationSource);
                }
            }

            try {
                if (!this.f) {
                    v2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }

                this.startActivity(v2);
                v0 = true;
            } catch (Exception v1) {
            }
        }

        return v0;
    }

    public final long d(String arg5) {
        long v0 = this.e(arg5);
        if (v0 == -1) {
            v0 = 0;
        }

        return v0;
    }

    public final long e(String arg3) {
        long v0_1;
        try {
            v0_1 = Long.valueOf(this.extraSchemaUri.getQueryParameter(arg3)).longValue();
        } catch (Exception v0) {
            v0_1 = -1;
        }

        return v0_1;
    }

    public final int f(String arg2) {
        int v0_1;
        try {
            v0_1 = Integer.valueOf(this.extraSchemaUri.getQueryParameter(arg2)).intValue();
        } catch (Exception v0) {
            v0_1 = -1;
        }

        return v0_1;
    }

    public void finish() {
        boolean v0 = this.mIsOverrideAnimation;
        this.mIsOverrideAnimation = true;
        super.finish();
        if (v0 != this.mIsOverrideAnimation) {
            this.mIsOverrideAnimation = v0;
        }
    }

    public final String g(String arg2) {
        String v0_1;
        try {
            v0_1 = this.extraSchemaUri.getQueryParameter(arg2);
        } catch (Exception v0) {
            v0_1 = "";
        }

        return v0_1;
    }

    public static boolean h(String arg1) {
        boolean v0 = (android.support.design.a.isTextEmpty(arg1)) || !"1".equals(arg1) ? false : true;
        return v0;
    }

    public void onCreate(Bundle savedInstanceState) {
        long v6 = 0;
        int v13 = -1;
        super.onCreate(savedInstanceState);
        if (Logger.debug()) {
            Logger.d("AdsAppBaseActivity", "onCreate start task_id = " + this.getTaskId());
        }

        this.e = Spipe.inst();
        this.j = BaseAppData.inst();
        if (!this.k) {
            BaseAppData.inst().b(((Context) this));
            this.k = true;
        }

        Intent v11 = this.getIntent();
        if (v11 == null) {
            if (this.isFinishing()) {
                return;
            }

            this.finish();
            return;
        }

        String v0 = v11.getAction();
        this.f = v11.getBooleanExtra("is_from_self", false);
        if (!android.support.design.a.isTextEmpty(v0) && v0.indexOf("com.ss.android.sdk.") == 0) {
            v0 = v11.getStringExtra("open_url");
            if (!android.support.design.a.isTextEmpty(v0)) {
                this.extraSchemaUri = Uri.parse(v0);
            }
        }

        if (this.extraSchemaUri == null) {
            this.extraSchemaUri = v11.getData();
        }

        if (this.extraSchemaUri == null) {
            if (this.isFinishing()) {
                return;
            }

            this.finish();
            return;
        }

        long v4 = this.e("adId");
        v0 = this.g("log_extra");
        JSONObject v8 = new JSONObject();
        try {
            if (android.support.design.a.isTextEmpty(v0)) {
//                goto label_65;
            } else {
                v8.put("log_extra", v0);
            }
        } catch (JSONException v0_1) {
            v0_1.printStackTrace();
        }

        label_65:
        v0 = this.extraSchemaUri.getHost();
        if ("sdkfeed".equals(v0)) {
            MobClickCombiner.onEvent(this, "embeded_ad", "sdk_appback", v4, v6, v8);
        } else if ("sdkdetail".equals(v0)) {
            MobClickCombiner.onEvent(this, "detail_ad", "sdk_appback", v4, v6, v8);
        } else if ("openurlfeed".equals(v0)) {
            MobClickCombiner.onEvent(this, "embeded_ad", "open_url_appback", v4, v6, v8);
        } else if ("openurldetail".equals(v0)) {
            MobClickCombiner.onEvent(this, "detail_ad", "open_url_appback", v4, v6, v8);
        }

        this.d = v11.getStringExtra("bundle_download_app_log_extra");
        this.g = v11.getBooleanExtra("from_notification", false);
        boolean v1 = ComponentsManifest.findComponent(com.ss.android.newmedia.c.a.class).b();
        boolean v8_1 = ComponentsManifest.findComponent(com.ss.android.newmedia.c.a.class).c();
        if (this.g) {
            this.notificationSource = this.g("source");
            try {
                this.i = v11.getIntExtra("msg_from", -1);
                this.l = v11.getIntExtra("msg_id", -1);
                com.ss.android.newmedia.message.b.a(((Context) this)).a(this.l);
                this.n = v11.getBooleanExtra("is_strong_message", false);
                this.m = v11.getStringExtra("msg_post_back");
                JSONObject v12 = new JSONObject();
                v12.put("isStrong", this.n);
                v12.put("rule_id", this.l);
                boolean v4_1 = false;
                switch (this.i) {
                    case 1: {
                        if (!v1) {
                            v4_1 = true;
                            break;
                        } else {
                            com.ss.android.newmedia.message.i.a(this, "news_notify_view", ((long) this.l), -1, false, new JSONObject[0]);
                            v4_1 = true;
                            break;
                        }
                    }
                    case 2: {
                        if (v1) {
                            com.ss.android.newmedia.message.i.a(this, "news_alert_view", ((long) this.l), -1, false, new JSONObject[0]);
                        }
                        v4_1 = false;
                        break;
                    }
                    default: {
                        break;
                    }
                }

                try {
                    label_120:
                    if (v8_1) {
                        JSONObject v6_1 = new JSONObject(v12.toString());
                        if (((com.ss.android.newmedia.c.a) ComponentsManifest.findComponent(com.ss.android.newmedia.c.a.class)).d()) {
                            v6_1.put("_staging_flag", 1);
                        }

                        SsPushManager.inst().trackClickPush(this, ((long) this.l), v4_1, this.m, v6_1);
                    }

                    int v0_3 = v11.getIntExtra("message_from", -1);
                    String v1_1 = v11.getStringExtra("message_extra");
                    if (v0_3 != v13 && !android.support.design.a.isTextEmpty(v1_1)) {
                        SsPushManager.inst().trackPush(this.getApplicationContext(), v0_3, v1_1);
                    }

                    switch (v11.getIntExtra("notification_image_type", 0)) {
                        case 1: {
                            v0 = "big_picture";
                            break;
                        }
                        case 2: {
                            v0 = "small_picture";
                            break;
                        }
                        default: {
                            v0 = "normal";
                            break;
                        }
                    }

                    v8 = new JSONObject();
                    v8.put("push_type", v0);
                    com.ss.android.newmedia.message.i.a(this, "news_push_click", ((long) this.l), -1, false, new JSONObject[]{v8});
                } catch (Exception v0_2) {
                    v0_2.printStackTrace();
                }
            } catch (Exception v0_2) {
                v0_2.printStackTrace();
            }
        }
        this.b = this.extraSchemaUri.getHost();
        this.c = this.extraSchemaUri.getPath();
        com.ss.android.newmedia.util.f.a();
        if (!this.d()) {
            this.startAppActivity();
        }
        if (Logger.debug()) {
            Logger.d("AdsAppBaseActivity", "onCreate end");
        }
        this.finish();
    }

    public void onNewIntent(Intent arg4) {
        super.onNewIntent(arg4);
        if (Logger.debug()) {
            Logger.d("AdsAppBaseActivity", "onNewIntent start task_id = " + this.getTaskId());
        }
    }

    public void onPause() {
        super.onPause();
        if (Logger.debug()) {
            Logger.d("AdsAppBaseActivity", "onPause start task_id = " + this.getTaskId());
        }
    }
}

